const CONTEXT = require('../../utils/public_var.js')
exports.uploadImg = function (that,app) {
  var context = that
  if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
    wx.showModal({
      title: '',
      content: '请您登录验证',
      confirmText: '登录',
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/login/atLogin/atLogin',
          })
        }
      }
    })
    return
  }
  if (context.data.totalCount > 0) {

    wx.chooseMedia({
      count: 1,
      sizeType: ['compressed'],
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgs = res.tempFiles;
        const child = context.selectComponent('.bottom_class');
        child.addUpImg(imgs[0].tempFilePath);
      },
    })
  } else {
    // 没有免费使用次数了
    wx.showModal({
      title: '体验用尽',
      content: '在下养家糊口，\n恳请主公雇佣在下',
      confirmText: "雇佣",
      cancelText: "取消",
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/sita/pay/paybill',
          })
        }
      }
    })
  }
}

exports.initImgMsgData = function(that,data,app){
  var chatMsg =  app.getMsg(that.data.bar_type)
  console.log(chatMsg)
  if (chatMsg != null && chatMsg != undefined && chatMsg.length > 0) {
    that.setData({
      MessageList: chatMsg
    })
    that.setData({
      intoViewName: 'endview'
    })
  }else{
  var initData = [{
    "role": 'system',
    "content": data,
    "upImgUrl":"",
    "msgtype":CONTEXT.TEXT
  }]
  // console.log(initData)
  that.setData({
    MessageList:initData
  })
}
}
exports.sendImgTextMsg = function(that,imgSrc,app,https_url) {
  var context = that
  var mathImg = {
    "role": 'user',
    "content": context.data.input_tx,
    "upImgUrl":imgSrc,
    "msgtype":CONTEXT.TEXT
    
  }
var list = context.data.MessageList
list.push(mathImg)
context.setData({
  showCrop: true,
  MessageList: list
})
context.setData({
  intoViewName: 'endview',
  
})
context.hintAll()
wx.showLoading({
  title: '识别中',
})
let requestBody = {
  'prompt': [{
    "role": 'user',
    "content": context.data.input_tx,
    "msgtype":CONTEXT.TEXT
    },
    
],
'openid': app.globalData.OpenID
}
let jsonData = JSON.stringify(requestBody)

wx.uploadFile({
  url: https_url,
  filePath: imgSrc,
  name: 'mediafile',
  formData: {
    'chatStringBody':jsonData
},
  timeout:60000,
  success: (res) => {
    wx.hideLoading();
    console.log(res);
    let backData =  JSON.parse(res.data);
    console.log(backData);
    if(backData.statusCode==200){
  
     var item = {
      "role": 'system',
      "content": backData.completeOpenAnswer.gptAnswer,
      "msgtype":CONTEXT.TEXT
   
    }
    console.log(item);
    var list = context.data.MessageList
    list.push(item)
    context.setData({
      showCrop: true,
      MessageList: list,
      totalCount:backData.completeOpenAnswer.remainingGPTCount
    })
    app.setCount(backData.completeOpenAnswer.remainingGPTCount)
    app.saveMsg(that.data.bar_type,list)
    context.setData({
      intoViewName: 'endview'
    })
    context.initSendUI()
      
   }
  },
  fail: (res) => {
    wx.hideLoading();
    console.log('fail:' + res);
  },
});
}

